package main;

import java.util.HashSet;
import java.util.Set;

import statemachine.Activity;
import statemachine.ActivityTransition;
import statemachine.NamedElement;
import statemachine.State;
import statemachine.StateTransition;
import statemachine.StateType;
import statemachine.Statemachine;

public class Validator {

	
	public boolean nameDefined(NamedElement namedElement) {
		return (namedElement.getName() != null)
				&& !namedElement.getName().equals("");
	}

	
	public boolean onlyOneTransitionPerEvent(State state) {
		Set<String> results = new HashSet<String>();

		for (StateTransition stateTransition : state.getStateTransitions()) {
			boolean notThereAlready = results.add(stateTransition.getEvent());
			if (!notThereAlready)
				return false;

		}
		return true;

	}
	
	
	public boolean onlyOneTransitionPerResult(Activity activity) {
		Set<String> results = new HashSet<String>();

		for (ActivityTransition activityTransition : activity
				.getActivityTransitions()) {
			boolean notThereAlready = results.add(activityTransition
					.getResult());
			if (!notThereAlready)
				return false;

		}
		return true;

	}

	public boolean oneStartState(Statemachine st) {
		int count = 0;
		for (State s : st.getStates()) {
			if (s.getType() == StateType.START)
				count++;

		}

		return count == 1;

	}

	public boolean atLeastOneStopState(Statemachine st) {
		int count = 0;
		for (State s : st.getStates()) {
			if (s.getType() == StateType.STOP)
				count++;

		}

		return count >= 1;

	}

	public boolean atLeastOneTransitionIfTypeIsNotStop(State state) {
		if (state.getType() != StateType.STOP) {

			return state.getStateTransitions().size() >= 1;

		}

		return true;

	}

	public boolean noTransitionsToAStartState(Activity activity) {
		for (ActivityTransition at : activity.getActivityTransitions()) {
			if (at.getActivityToStateTransition().getType() == StateType.START) {
				return false;
			}
		}

		return true;

	}

	public boolean noTransitionsFromAStopState(State state) {
		if (state.getType() == StateType.STOP) {
			return state.getStateTransitions().isEmpty();
		}

		return true;

	}

}
